Skip to content

RAD-17385 iOS: keep the first attempt's result when it arrives after a re-send - #83

Open
dmurphy5 wants to merge 4 commits into
masterfrom
dylan/ios-late-result-wins
Open

dmurphy5 wants to merge 4 commits into
masterfrom
dylan/ios-late-result-wins

Conversation

@dmurphy5

Copy link
Copy Markdown

What was wrong

After a relaunch, iOS replays the results of uploads that finished while the app was not running. The library waits 10 s (graceMs) for each one. When no result came in that time, it deleted the old task's key and sent the request again as a new attempt (task B).

When the first attempt's result then arrived, the library could not match it to the entry any more, so it dropped it. That included a 2xx. B then ran to the end. So a first attempt that the server accepted was always followed by a second send, which can store a second copy on the server.

What changes

  • A kept key with its own purpose. When the grace ends, withoutTask (QueueCoordinator+Reconcile.swift) marks the old attempt's keys lateResult instead of deleting them. A key already marked superseded or pause keeps its purpose, because its task may still owe a cancel callback. The re-send is unchanged.
  • A late result. taskCompletedLocked treats lateResult like superseded:
    • an accepted result on a live entry completes it, and settle cancels B;
    • a late failure or cancel is dropped, and B goes on with no retry or event for the old attempt;
    • a result after the entry settled is dropped, so the app gets one outcome.
  • Reconcile. It never adopts a task with a lateResult key. If it finds one live, it cancels it and changes the key to superseded, because that task now owes a cancel callback. A task that iOS lists as finished is not live, so reconcile neither adopts nor cancels it.
  • When the kept keys go. Every lateResult key of an id is removed when the id settles, when its row is forgotten (the ack that forgets it, or cancel() on a settled row), and when a same-id mutate() gives it a different body. This covers any attempt and any generation. Before, the end of the 10 s wait deleted every key of the waited attempt, including keys of tasks the library had cancelled (superseded or pause). Now such a key stays until its task's cancel callback removes it, or until the row is gone and the next launch prunes it.
  • The winning request id. A simple request's outcome now carries the request id of the attempt whose result settled it, as the chunked path already does. Only an accepted result changes the entry's request id. So the response and requestId of an outcome belong to the same request. attempts still counts every attempt.
sequenceDiagram
    participant D as iOS daemon
    participant L as Library
    participant S as Server
    Note over D,S: Attempt 1 finished while the app was not running
    L->>L: Relaunch: wait 10 s for attempt 1's result
    L->>L: No result: keep attempt 1's key as a late-result key
    L->>D: Create task B (attempt 2)
    D-->>L: Late result of attempt 1: 201
    L->>L: Complete the entry with attempt 1's response and request id
    L->>D: Cancel task B
    Note over D,S: If B had not started, the server gets one request
Loading

Behavior change

  • Late results (iOS). A late accepted result (a 2xx, or a status the definition's accept rules allow) now completes an entry that earlier versions left running and sent again. The app gets the first attempt's response. The late result emits an attempt event for attempt 1 with its X-Request-Id.
  • requestId (iOS, simple requests). The settled outcome's requestId is now the id of the attempt whose result settled the entry. Before, it was the last attempt's, which after a late result could be a task that was cancelled before it began.
  • Android, read from the code only. The settled record uses lastRequestId, which is set when each attempt starts. A simple request on Android runs one attempt at a time, so its last attempt is the one that settled it. A chunked request runs parts at the same time, so its requestId is the last part attempt that started, which can be another part.
  • Docs. The Meta.requestId doc in src/types.ts and the README say this per platform. The CHANGELOG has the entry under "Unreleased", and the README "Relaunch" paragraph describes the late result.

What it does not fix

  • The second send still happens after 10 s. If B already sent its request when the late result arrives, the server still gets two requests. The cancel only helps when B has not started yet, for example a task created in the background that iOS has not started.
  • The same attempt of a later use of an id. A result from an earlier use of an id can still reach a later use at the same attempt number. The task description alone (id, generation, attempt) matches it, with or without a key. This needs a per-use value, which this PR does not add.
  • Tracked as separate tasks:
    • a pause and resume inside the 10 s wait;
    • an entry that passes expiresAt during the wait;
    • the completion handler held for the rest of a later launch's wait;
    • holding the slot while iOS lists the old task as finished.
  • Unchanged: the 10 s wait, chunked parts (a late accepted part already wins) and Android.

What Apple documents and what is observed. Apple documents that the session replays didCompleteWithError for finished tasks after the app creates the session again, and that a cancelled task always gets a callback. Apple does not document how late a replayed result can arrive, or that a task listed as finished still gets its callback. One observation (RAD-17385) shows the library re-sent a task that iOS listed as finished. This change does not depend on either undocumented point: it only keeps a result that does arrive.

Tests

24 new tests in CoordinatorLateResultTests, and two updated tests in CoordinatorRelaunchTests. They cover:

  • A late accepted result wins: with the second task live, in backoff, paused, awaiting auth, after a second relaunch, and after two re-issues. The main test failed on master.
  • Late results that are dropped: a 500, a transport error and a cancel, each on its own kept key; a 2xx after the second attempt completed; a 2xx after a user cancel.
  • The kept keys are removed at settle, at the repair of a lost settle, at the ack, at cancel() of a settled row, and at a different-body replace.
  • A later use of the same id is not completed by an earlier use's late result (three uses of one id).

Each part of the fix was broken on purpose, and at least one test failed each time.

Checks run locally, all exit 0: yarn lint:ci, yarn typecheck, yarn test (305 tests), swift test in ios (407 tests, 1 perf test skipped by design), the Android unit tests (424 tests).

Two independent reviews were applied. Part of RAD-17385.

🤖 Generated with Claude Code

dmurphy5 and others added 4 commits November 17, 2026 16:38
After a relaunch, the library waits 10 s for the result of a task that
finished while the app was not running. When none came, it deleted the
task's key and sent the request again as a new attempt. A late result of
the first attempt was then dropped, a 2xx included, and the second send
ran to the end.

Now the grace marks the old keys superseded instead of deleting them. A
late accepted result completes the entry with the first attempt's
response, and settle cancels the new attempt's task. A late failure or
cancel of the old attempt is dropped and does not disturb the new one.
settle (and the relaunch repair of a lost settle) removes an entry's
superseded keys of older attempts, so the task map does not grow.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…g request

The relaunch grace kept the old attempt's key as superseded, and settle
removed only keys of the same generation with a lower attempt. A key
stayed when the entry settled inside the grace's own re-issue, or when a
different body replaced the entry. A later use of the id, which starts
again at generation 1, could then be completed by the old late result.
The settle cleanup also removed superseded keys of tasks that reconcile
had cancelled, so their cancel callback could reach a later use of the id
as a system cancel and send it twice.

Now a kept key has its own purpose, lateResult. It is handled like
superseded when a late result is evaluated. Reconcile turns it into
superseded when it finds the task live, because the task then owes a
cancel callback. Every lateResult key of an id is removed when the id
settles, is forgotten (ack or cancel of a settled row), or gets a
different body. Keys of other purposes stay until their callback.

A simple request's outcome now carries the request id of the attempt
whose result settled it, as the chunked path already does.

The late-result tests move to CoordinatorLateResultTests. Each late
failure kind gets its own kept key.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The copy of the completing task's request id into the entry now runs only
for an accepted result, so a result that does not settle the entry as
completed never changes its request id.

taskWillBegin, taskProgress and replaceBackgroundTasks now check
isOutdated, so a lateResult key is treated like a superseded one there.
No task holds a lateResult key in those paths today: reconcile changes a
live task's lateResult key to superseded.

The CHANGELOG now says that a superseded or pause key of the waited
attempt stays until its cancel callback, where the end of the wait used
to delete it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant